package com.lkd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.VmDTO;
import com.lkd.entity.*;
import com.lkd.mapper.*;
import com.lkd.result.TableInfo;
import com.lkd.service.VmService;
import com.lkd.utils.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Z.lx
 * @Date: 2025/8/17 21:20
 * @Description:
 **/
@Service
public class VmServiceImpl implements VmService {
    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private BusinessTypeMapper businessTypeMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;

    /**
     * @Description 列表查询
     * @Param [vmDTO]
     * @Date 2025/8/17 21:23
     **/
    @Transactional
    public TableInfo<VM> list(VmDTO vmDTO) {
        PageHelper.startPage(vmDTO.getPageIndex(), vmDTO.getPageSize());
        List<VM> vmList = vmMapper.getByInnerCodeId(vmDTO.getInnerCode());
        vmList.forEach(vm -> {
            Region region = regionMapper.getRegionById(vm.getRegionId());
            Region regionPlus = new Region();
            BeanUtils.copyProperties(region,regionPlus);
            Business businessType = businessTypeMapper.getById(vm.getBusinessId());
            Node node = nodeMapper.getByNodeId(vm.getNodeId());
            Node nodePlus = new Node();
            BeanUtils.copyProperties(node, nodePlus);
            Integer nodeCount = nodeMapper.getNodeCount(vm.getNodeId());
            VmType vmType = vmTypeMapper.getById(String.valueOf(vm.getVmType()));
            vm.setType(vmType);
            vm.setRegion(regionPlus);
            String status= vmMapper.getStatusByVmId(vm.getRunStatus());
            Map<String,Boolean> status1 = new HashMap<>();
            status1.put("10001",status.equals("10001"));
            status1.put("10002",status.equals("10002"));
            status1.put("10003",status.equals("10003"));
            vm.setStatus(status1);
            vm.setNode(node);
        });
        Page<VM> page = (Page<VM>) vmList;
        return new TableInfo<>(
                page.getPageNum(),
                page.getPageSize(),
                page.getPages(),
                page.getTotal(),
                page.getResult());
    }

    public VmType add(VmDTO vmDTO) {
        String innerCode = UUIDUtils.getUUID();

        VM vm = new VM();
        VmType vmType = vmTypeMapper.getById(vmDTO.getVmType());
        Node node = nodeMapper.getByNodeId(vmDTO.getNodeId());
        vm.setRegionId(node.getRegionId());
        vm.setInnerCode(innerCode);
        vm.setCityCode("010");
        vm.setNodeId(node.getId());
        BeanUtils.copyProperties(node, vm,"id");
        vm.setVmType(vmType.getTypeId());
        vmDTO.setCreateUserId(vmDTO.getCreateUserId());
        vmMapper.insert(vm);
        return vmType;
    }

    @Override
    public boolean update(String id, String nodeId) {
        return vmMapper.updateVm(id, nodeId);
    }
}
